﻿using System;
using System.Configuration;
using System.Xml;

namespace SwissTV.Entities.Social
{
    public class SocialConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("authentication")]
        public AuthenticationElement Authentication { get { return (AuthenticationElement)this["authentication"]; } }

        [ConfigurationProperty("urls")]
        [ConfigurationCollection(typeof(UrlElement), AddItemName = "url")]
        public UrlCollection Urls { get { return (UrlCollection)this["urls"]; } }
    }

    public class ConfigurationTextElement<T> : ConfigurationElement
    {
        private T _value;
        protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            _value = (T)reader.ReadElementContentAs(typeof(T), null);
        }
        public T Value
        {
            get { return _value; }
        }
    }

    public class AuthenticationElement : ConfigurationElement
    {
        [ConfigurationProperty("mode")]
        public string Mode
        {
            get
            {
                return this["mode"].ToString();
            }
            set
            {
                this["mode"] = value;
            }
        }

        [ConfigurationProperty("url")]
        public ConfigurationTextElement<string> Url
        {
            get { return (ConfigurationTextElement<string>)this["url"]; }
        }

        [ConfigurationProperty("userid")]
        public ConfigurationTextElement<string> UserID
        {
            get { return (ConfigurationTextElement<string>)this["userid"]; }
        }

        [ConfigurationProperty("login")]
        public ConfigurationTextElement<string> Login
        {
            get { return (ConfigurationTextElement<string>)this["login"]; }
        }

        [ConfigurationProperty("password")]
        public ConfigurationTextElement<string> Password
        {
            get { return (ConfigurationTextElement<string>)this["password"]; }
        }

        [ConfigurationProperty("applicationcode")]
        public ConfigurationTextElement<string> ApplicationCode
        {
            get { return (ConfigurationTextElement<string>)this["applicationcode"]; }
        }

        [ConfigurationProperty("key")]
        public ConfigurationTextElement<string> Key
        {
            get { return (ConfigurationTextElement<string>)this["key"]; }
        }

        [ConfigurationProperty("hash")]
        public ConfigurationTextElement<string> Hash
        {
            get { return (ConfigurationTextElement<string>)this["hash"]; }
        }

        public AuthenticationInfo getAuthentication()
        {
            string correctFormat = Mode.ToLower();
            correctFormat =  char.ToUpper(correctFormat[0]) + correctFormat.Substring(1);
            AuthenticationMode modeValue = (AuthenticationMode)Enum.Parse(typeof(AuthenticationMode), correctFormat);

            return new AuthenticationInfo(modeValue, Url.Value, UserID.Value, Login.Value, Password.Value, ApplicationCode.Value, Key.Value, Hash.Value);
        }

    }

    public class UrlElement : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public String Name
        {
            get { return this["name"].ToString(); }
            set { this["name"] = (string)value; }
        }

        [ConfigurationProperty("value")]
        public String Value
        {
            get { return this["value"].ToString(); }
            set { this["value"] = (string)value; }
        }
    }

}

